﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class ThietLapThuongLuong : System.Web.UI.Page
{
    int pc;
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmThietLapThuong";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 6;
    static int CurrentPage = 1;
    int dachi;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    btnCancel_Click(sender, e);

                    Title = "Quản lý thưởng lương | " + Header;
                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {

                        //drnam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;
                    dlNam.SelectedValue = year2.ToString();

                    txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dlNam.Visible = false;
                    btnCancel_Click(sender, e);
                    LoadNhanVien();
                    loadnhanvientheothoigian();
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                Guid thietlap = Guid.NewGuid();
                p = new SqlParameter[8];
                p[0] = new SqlParameter("@IDs", thietlap);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@HoTen", ex.GetGuid(drhoten.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                p[3].SqlDbType = SqlDbType.NVarChar;
                p[4] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Replace(",", "")));
                p[4].SqlDbType = SqlDbType.Float;
                p[5] = new SqlParameter("@QuyetDinh", txtquyetdinh.Text.Trim());
                p[5].SqlDbType = SqlDbType.NVarChar;
                p[6] = new SqlParameter("@DaChi", ckdachi.Checked ? 1 : 2);
                p[6].SqlDbType = SqlDbType.Int;
                p[7] = new SqlParameter("@Del", 1);
                p[7].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_XDAThuongLuong_Add", p);
                if (ckdachi.Checked)
                {
                    Guid tlchi = Guid.NewGuid();
                    p = new SqlParameter[12];
                    p[0] = new SqlParameter("@IDs", tlchi);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drhoten.SelectedValue));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                    p[4].SqlDbType = SqlDbType.NVarChar;

                    p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                    p[5].SqlDbType = SqlDbType.SmallDateTime;
                    p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                    p[6].SqlDbType = SqlDbType.NVarChar;
                    p[7] = new SqlParameter("@Del", 1);
                    p[7].SqlDbType = SqlDbType.Int;
                    p[8] = new SqlParameter("@IDThanhToan", thietlap);
                    p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[9] = new SqlParameter("@UngLuong", DBNull.Value);
                    p[9].SqlDbType = SqlDbType.Int;
                    p[10] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                    p[10].SqlDbType = SqlDbType.Int;
                    p[11] = new SqlParameter("@NopNganHang", DBNull.Value);
                    p[11].SqlDbType = SqlDbType.Int;

                    ex.ExecuteNonQuerry("sp_xdaThietLapPhieuChiTienMat_Add", p);
                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                    dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat", p);
                    int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    if (sopc == 0)
                        pc = 1;
                    else pc = sopc + 1;
                    p = new SqlParameter[11];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                    p[2] = new SqlParameter("@NoiDung", DBNull.Value);
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@SoPC", pc);
                    p[4].SqlDbType = SqlDbType.Int;
                    p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[6] = new SqlParameter("@Del", 1);
                    p[6].SqlDbType = SqlDbType.Int;
                    p[7] = new SqlParameter("@LoaiCongNo", 2);
                    p[7].SqlDbType = SqlDbType.Int;
                    p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[8].SqlDbType = SqlDbType.Float;
                    p[9] = new SqlParameter("@IDLenhChi", tlchi);
                    p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drhoten.SelectedValue));
                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);

                    p = new SqlParameter[4];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@IDPhieuChi", tlchi);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@Del", 1);
                    p[3].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdanoidungpc_Add", p);

                }
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thưởng\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        drhoten.SelectedIndex = 0;
        txtbophan.Text = "";
        txtchucvu.Text = "";
        txtsotien.Text = "";
        txtquyetdinh.Text = "";
        txtnoidung.Text = "";
        txtngaythang.Text = "";
        txtngaythang.Enabled = true;

        hdID.Value = "";
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_XDAThuongLuong_List", p);
        GV.DataBind();
        btnSave.Visible = true;
        btncapnhat.Visible = false;
        txtbophan.Enabled = false;
        txtchucvu.Enabled = false;
        ckdachi.Checked = false;
        //btnSave.Visible = CheckKhoa2(txtngaythang.Text.Trim(), frmName, Session["user"].ToString());
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_XDAThuongLuong_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    drhoten.SelectedValue = dt.Rows[0]["hoten"].ToString();
                    drhoten_SelectedIndexChanged(sender, e);
                    // drsotaikhoan.Items = dt.Rows[0]["taikhoan"].ToString();
                    txtngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                    txtquyetdinh.Text = dt.Rows[0]["quyetdinh"].ToString();
                    txtsotien.Text = dt.Rows[0]["sotien"].ToString();
                    txtnoidung.Text = dt.Rows[0]["noidung"].ToString();
                    if (dt.Rows[0]["dachi"].ToString().Equals("1"))
                        ckdachi.Checked = true;
                    else ckdachi.Checked = false;
                    HiddenField1.Value = dt.Rows[0]["dachi"].ToString();
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thưởng\t" + ax.Message);
                }

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
            btncapnhat.Visible = CheckKhoa2(txtngaythang.Text.Trim(), frmName, Session["user"].ToString());
            if (btncapnhat.Visible == false)
            {
                txtngaythang.Enabled = false;
            }
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_XDAThuongLuong_DelPhieuChi", p);
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_XDAThuongLuong_DelThietLap", p);
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_xdathuongluong_Del", p);


                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess("Có lỗi trong quá trình xóa, xem trong file log", "");
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thưởng\t" + ax.Message);

                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID = new Guid(hdID.Value);
                dachi = Int16.Parse(HiddenField1.Value);

                if (dachi == 1 && ckdachi.Checked == true)
                {

                    p = new SqlParameter[10];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drhoten.SelectedValue));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                    p[4].SqlDbType = SqlDbType.NVarChar;

                    p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                    p[5].SqlDbType = SqlDbType.SmallDateTime;
                    p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                    p[6].SqlDbType = SqlDbType.NVarChar;
                    p[7] = new SqlParameter("@Del", 1);
                    p[7].SqlDbType = SqlDbType.Int;
                    p[8] = new SqlParameter("@UngLuong", DBNull.Value);
                    p[8].SqlDbType = SqlDbType.Int;
                    p[9] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                    p[9].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdathietlapphieuchitienmat_Update_ThuongLuong", p);
                    p = new SqlParameter[3];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                    p[2] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.Float;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpDate_PCCongNo_ThuongLUong", p);
                    p = new SqlParameter[2];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpDate_MoiDungPC_ThuongLUong", p);
                  
                }
                else if (dachi == 1 && ckdachi.Checked == false)
                {
                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_XDAThuongLuong_DelPhieuChi", p);
                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_XDAThuongLuong_DelThietLap", p);
                }
                else if(dachi==2 && ckdachi.Checked==true){
                    Guid tlchi = Guid.NewGuid();
                    p = new SqlParameter[12];
                    p[0] = new SqlParameter("@IDs", tlchi);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drhoten.SelectedValue));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                    p[4].SqlDbType = SqlDbType.NVarChar;

                    p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                    p[5].SqlDbType = SqlDbType.SmallDateTime;
                    p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                    p[6].SqlDbType = SqlDbType.NVarChar;
                    p[7] = new SqlParameter("@Del", 1);
                    p[7].SqlDbType = SqlDbType.Int;
                    p[8] = new SqlParameter("@IDThanhToan", gID);
                    p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[9] = new SqlParameter("@UngLuong", DBNull.Value);
                    p[9].SqlDbType = SqlDbType.Int;
                    p[10] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                    p[10].SqlDbType = SqlDbType.Int;
                    p[11] = new SqlParameter("@NopNganHang", DBNull.Value);
                    p[11].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdaThietLapPhieuChiTienMat_Add", p);
                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                    dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat", p);
                    int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    if (sopc == 0)
                        pc = 1;
                    else pc = sopc + 1;
                    p = new SqlParameter[11];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                    p[2] = new SqlParameter("@NoiDung", DBNull.Value);
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@SoPC", pc);
                    p[4].SqlDbType = SqlDbType.Int;
                    p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[6] = new SqlParameter("@Del", 1);
                    p[6].SqlDbType = SqlDbType.Int;
                    p[7] = new SqlParameter("@LoaiCongNo", 2);
                    p[7].SqlDbType = SqlDbType.Int;
                    p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[8].SqlDbType = SqlDbType.Float;
                    p[9] = new SqlParameter("@IDLenhChi", tlchi);
                    p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drhoten.SelectedValue));
                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);

                    p = new SqlParameter[4];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@IDPhieuChi", tlchi);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@Del", 1);
                    p[3].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdanoidungpc_Add", p);
                }
                p = new SqlParameter[8];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@HoTen", ex.GetGuid(drhoten.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                p[3].SqlDbType = SqlDbType.NVarChar;
                p[4] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Replace(",", "")));
                p[4].SqlDbType = SqlDbType.Float;
                p[5] = new SqlParameter("@QuyetDinh", txtquyetdinh.Text.Trim());
                p[5].SqlDbType = SqlDbType.NVarChar;
                p[6] = new SqlParameter("@DaChi", ckdachi.Checked ? 1 : 2);
                p[6].SqlDbType = SqlDbType.Int;
                p[7] = new SqlParameter("@Del", 1);
                p[7].SqlDbType = SqlDbType.Int;

                ex.ExecuteNonQuerry("sp_XDAThuongLuong_Update", p);
                btnCancel_Click(sender, e);
                gstGetMess("Cập nhật thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thưởng\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền sửa dữ liệu trên trang này", "");
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_XDAThuongLuong_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_XDAThuongLuong_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_XDAThuongLuong_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    private bool CheckKhoa2(string ngaythang, string name, string nhanvien)
    {
        if (!ngaythang.Trim().Equals(""))
        {

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@Name", name);
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@Ngay", ex.GetNgayThang(ngaythang));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@NhanVien", nhanvien);
            p[2].SqlDbType = SqlDbType.NVarChar;

            string index = ex.GetValueString("sp_KhoaMo_Check", p);

            if (index.Trim().Equals("2"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else return false;
    }

    void LoadNhanVien()
    {
        drhoten.Items.Clear();
        drhoten.DataSource = ex.GetData("sp_XDAThuongLuong_ListNhanVien");
        drhoten.DataBind();
        drhoten.Items.Insert(0, new ListItem("--Chọn họ tên--", ""));
        drhoten.SelectedIndex = 0;
    }



    protected void drhoten_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", ex.GetGuid(drhoten.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        DataTable dt = ex.GetData("xdaThuongLuong_ListBoPhan", p);
        if (dt.Rows.Count > 0)
        {
            try
            {
                txtbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                txtchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thưởng\t" + ax.Message);
            }
        }
    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
       
        loadnhanvientheothoigian();
    }

    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        getDate();
        if (dropNhanVien.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;

            GV.DataSource = ex.GetData("sp_xdathuongluong_List_TheoThoiGian", p);
            GV.DataBind();
        }
        else {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@NhanVien", ex.GetGuid(dropNhanVien.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GV.DataSource = ex.GetData("sp_xdathuongluong_List_TheoThoiGian_NhanVien", p);
            GV.DataBind();
        }
        btnPre.Visible = false;
        btnNext.Visible = false;
        btnFirst.Visible = false;
    }

    public void loadnhanvientheothoigian() {

        getDate();
      
        p = new SqlParameter[2];
       
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        dropNhanVien.Items.Clear();
        dropNhanVien.DataSource = ex.GetData("sp_xdathuongluong_List_TheoThoiGian_ListNhanVien", p);
        dropNhanVien.DataBind();
        dropNhanVien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        dropNhanVien.SelectedIndex = 0;
        dropNhanVien.Visible = true;
    }
    protected void txtngaythang_TextChanged(object sender, EventArgs e)
    {   
        if (hdID.Value.Equals(""))
            btnSave.Visible = CheckKhoa2(txtngaythang.Text.Trim(), frmName, Session["user"].ToString());
        else
            btncapnhat.Visible = CheckKhoa2(txtngaythang.Text.Trim(), frmName, Session["user"].ToString());
    }
}